import 'package:flutter/material.dart';

class CustomerEdit extends StatefulWidget {
  @override
  _CustomerEditState createState() => _CustomerEditState();
}

class _CustomerEditState extends State<CustomerEdit> {
  List<Map<String, dynamic>> maps = new List();
  List<Widget> items = new List();
  var titles = ['潜在', '闲置', '成交', '完工'];
  var title = [
    '建档人: ',
    '建档时间: ',
    '目前状态: ',
    '所属类别: ',
    '客户需求: ',
    '客户来源: ',
    '客户姓名: ',
    '客户电话1: ',
    '客户电话2: ',
    '建档人: ',
    '建档时间: ',
    '目前状态: ',
    '所属类别: ',
    '客户需求: ',
    '客户来源: ',
    '客户姓名: ',
    '客户电话1: ',
    '客户电话2: '
  ];
  var content = [
    '白洁',
    '2019-10-24',
    '潜在',
    '2019年客户',
    '背景音乐',
    '自然客流',
    '高义',
    '13488998899',
    '13488998890',
    '白洁',
    '2019-10-24',
    '潜在',
    '2019年客户',
    '背景音乐',
    '自然客流',
    '高义',
    '13488998899',
    '13488998890'
  ];

  _doWidgets() {
    if (maps.length == 0) {
      setMaps();
    }
    List<Widget> listTitles = new List();
    for (var i = 0; i < maps.length; i++) {
      if (i < 2) {
        listTitles.add(buildListItemText(context, maps[i]));
      } else if (i > 1 && i < 6) {
        listTitles.add(buildListItemGreen(context, maps[i]));
      } else {
        listTitles.add(buildListItemEdit(context, maps[i]));
      }

      if (i == 1) {
        listTitles.add(SizedBox(
          height: 10,
        ));
      }
    }
    return listTitles;
  }

  setMaps() {
    for (var i = 0; i < title.length; i++) {
      Map<String, dynamic> map = new Map();
      map['title'] = title[i];
      map['content'] = content[i];
      maps.add(map);
    }
  }

  Widget buildListItemText(BuildContext context, Map<String, dynamic> map) {
    return Column(
      children: <Widget>[
        Container(
          color: Colors.white,
          padding: EdgeInsets.all(15),
          child: Row(
            children: <Widget>[
              Text(
                map['title'],
                style: TextStyle(fontSize: 16, color: Colors.black87),
              ),
              SizedBox(
                width: 5,
              ),
              Text(
                map['content'],
                style: TextStyle(fontSize: 16, color: Colors.grey),
              ),
            ],
          ),
        ),
        Container(height: 1)
      ],
    );
  }

  setBottomSheet(List titles, Map<String, dynamic> map) {
    List<Widget> sheets = new List();
    for (var i = 0; i < titles.length; i++) {
      sheets.add(ListTile(
        title: Text(titles[i]),
        onTap: () {
          map['content'] = titles[i];
          setState(() {
            _doWidgets();
          });
          Navigator.pop(context);
        },
      ));
    }

    return sheets;
  }

  Widget buildListItemGreen(BuildContext context, Map<String, dynamic> map) {
    return Column(
      children: <Widget>[
        GestureDetector(
          onTap: () {
            showModalBottomSheet(
                context: context,
                builder: (BuildContext context) {
                  return Column(
                    mainAxisSize: MainAxisSize.min,
                    children: setBottomSheet(titles, map),
                  );
                });
          },
          child: Container(
            color: Colors.white,
            padding: EdgeInsets.all(15),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      '*',
                      style: TextStyle(fontSize: 16, color: Colors.red),
                    ),
                    Text(
                      map['title'],
                      style: TextStyle(fontSize: 16, color: Colors.black87),
                    ),
                    SizedBox(
                      width: 5,
                    ),
                    Text(
                      map['content'],
                      style: TextStyle(fontSize: 16, color: Colors.green),
                    ),
                  ],
                ),
                Icon(
                  Icons.keyboard_arrow_right,
                  size: 16,
                  color: Colors.black26,
                ),
              ],
            ),
          ),
        ),
        Container(height: 1)
      ],
    );
  }

  Widget buildListItemEdit(BuildContext context, Map<String, dynamic> map) {
    return Column(
      children: <Widget>[
        Container(
          color: Colors.white,
          padding: EdgeInsets.all(15),
          child: Row(
            children: <Widget>[
              Text(
                map['title'],
                style: TextStyle(fontSize: 16, color: Colors.black87),
              ),
              SizedBox(
                width: 5,
              ),
              Expanded(
                child: TextFormField(
                  initialValue: map['content'],
                  style: TextStyle(fontSize: 16, color: Colors.grey),
                  keyboardType: TextInputType.text,
                  decoration: InputDecoration(
                      contentPadding: const EdgeInsets.symmetric(vertical: 0),
                      hintText: '请输入${map['title']}',
                      hintStyle: TextStyle(color: Colors.black12, fontSize: 16),
                      border: InputBorder.none
//                        icon: Icon(Icons.text_fields),
//                        labelText: '请输入你的姓名)',
//                        helperText: '请输入你的真实姓名',
                      ),
                  autofocus: false,
                ),
              ),
            ],
          ),
        ),
        Container(height: 1)
      ],
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          '客户详情',
          style: TextStyle(color: Colors.black),
        ),
        backgroundColor: Colors.grey[200],
        leading: IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.black,
            ),
            onPressed: () {
              Navigator.pop(context);
            }),
        actions: <Widget>[
          IconButton(
              icon: Icon(
                Icons.save,
                color: Colors.green,
              ),
              onPressed: () {
                Navigator.pop(context);
              }),
        ],
        elevation: 0.0,
      ),
      body: Container(
        color: Colors.grey[200],
        child: ListView(children: _doWidgets()),
      ),
    );
  }
}
